<?php
/*
中介管理
*/

namespace Home\Controller;
use Think\Controller;
class MediaController extends CommonController
{
  //中介管理页面显示
    public function index(){
      $con = array('isdel'=>'0');
    if($_GET['p']==''){
        $p='1';
    }else{
        $p=$_GET['p'];
    }
    $count=D('Agent') -> countAgent($con);
    $n=15;
    $page=new \Think\Page($count,$n);
    $show= $page->show();// 分页显示输出
    $this->assign('page',$show);
      $this->assign('AgentNum',$count);
      $agents = D('Agent') -> selectAgent($con,$p,$n);
      $this->assign('agents' , $agents);
      $this->display('Media/index');
    }

    public function search(){
      if(I('post.agentname')){
        $con = array('isdel'=>'0','company_name'=>I('post.agentname'));
      }
      else{
        $con = array('isdel'=>'0');
      }
      if($_GET['p']==''){
        $p='1';
    }else{
        $p=$_GET['p'];
    }
    $count=D('Agent') -> countAgent($con);
    $n=15;
    $page=new \Think\Page($count,$n);
    $show= $page->show();// 分页显示输出
    $this->assign('page',$show);
      $this->assign('AgentNum',$count);
      $agents = D('Agent') -> selectAgent($con,$p,$n);
      $this->assign('agents' , $agents);
      $this->assign('searname',I('post.agentname'));
      $this->display('Media/index');
    }

  //中介添加
    public function mediationadd(){
      $this->display('Media/mediation-add');
    }

  //中介修改
    public function mediationalert(){
      $id = I('get.id');
      $agent = D('Agent')->findData($id);
      $this->assign('agent',$agent);
      $this->display('Media/mediation-alert');
    }

  //添加检查
    public function addCheck(){
      if(IS_POST){
        $point_id = D('Point') -> addPoint('Media/addCheck', '增加中介');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
          $data['company_name']=I('post.agentname');
          $findSame = D('Agent') -> findSameAgent($data['company_name']);
          if($findSame){
            $this->jsonReply('500','此中介已经添加');
          }
          $add = D('Agent')->addAgent($data);
          if($add){
            $this->jsonReply('200','添加成功');
          }
          else{
            $this->jsonReply('500','添加失败');
          }
        }
        else{
          $this -> jsonReply('500' , '您没有权限操作此功能');
        }
      }
    }

//编辑检验
    public function editCheck(){
      if(IS_POST){
        $point_id = D('Point') -> addPoint('Media/editCheck', '编辑中介');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
          $agentid = I('post.agentid');
          $data['company_name'] = I('post.agentname');
          if($data['company_name']){
            $findAgentName = D('Agent') -> findSameAgent($data['company_name'] , $agentid);
            if($findAgentName){
              $this -> jsonReply('500' , '此中介已被添加');
            }
            $add = D('Agent') -> saveAgent($agentid,$data);
            if($add){
              $this -> jsonReply('200' , '修改成功');
            }
            else{
              $this -> jsonReply('500' , '请对中介名称进行修改');
            }
          }
          else{
            $this -> jsonReply('500' , '中介名称不能为空');
          }
        }
        else{
          $this -> jsonReply('500' , '您没有权限操作此功能');
        }
      }
    }

    //删除检验
    public function delCheck(){
      if(IS_POST){
        $point_id = D('Point') -> addPoint('Media/delCheck', '删除中介');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
          $id = I('post.agentid');
          $del = D('Agent') -> saveAgent($id,array('isdel'=>'1'));
          if($del){
            $this -> jsonReply('200' , '删除成功');
          }
          else{
            $this -> jsonReply('500' , '删除失败');
          }
        }
        else{
          $this -> jsonReply('500' , '您没有权限操作此功能');
        }
      }
    }

//批量删除的检验
    public function delDatas(){
      if(IS_POST){
        $point_id = D('Point') -> addPoint('Media/delDatas', '批量删除中介');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
          $id = I('post.id');
          if(count($id)==1){
            $del = D('Agent') -> saveAgent($id[0],array('isdel'=>'1'));
          }
          if(count($id)>1){
            foreach($id as $k=>$v){
              $del = D('Agent') -> saveAgent($v,array('isdel'=>'1'));
            }
          }
          if($del){
            $this -> jsonReply('200' , '删除成功');
          }
          else{
            $this -> jsonReply('500' , '删除失败');
          }
        }
        else{
          $this -> jsonReply('500' , '您没有权限操作此功能');
        }
      }
    }








}
